package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil; 
import java.math.BigDecimal;
import java.util.Calendar;

@Repository("FoodsaleaddressDAO")
@Transactional
public class FoodsaleaddressDAOImpl extends AbstractJpaDao<Foodsaleaddress> implements FoodsaleaddressDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Foodsaleaddress.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public FoodsaleaddressDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Foodsaleaddress findFoodsaleaddressById(Integer id) throws DataAccessException {

		return findFoodsaleaddressById(id, -1, -1);
	}

	@Transactional
	public Foodsaleaddress findFoodsaleaddressById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodsaleaddressById", startResult, maxRows, id);
			return (Foodsaleaddress) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddressByName(String name) throws DataAccessException {

		return findFoodsaleaddressByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddressByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodsaleaddressByName", startResult, maxRows, name);
		return new LinkedHashSet<Foodsaleaddress>(query.getResultList());
	}

	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddressByNameContaining(String name) throws DataAccessException {

		return findFoodsaleaddressByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddressByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodsaleaddressByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Foodsaleaddress>(query.getResultList());
	}

	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddressByDate(Calendar date) throws DataAccessException {

		return findFoodsaleaddressByDate(date, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddressByDate(Calendar date, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodsaleaddressByDate", startResult, maxRows, date);
		return new LinkedHashSet<Foodsaleaddress>(query.getResultList());
	}

	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddressByPrice(BigDecimal price) throws DataAccessException {

		return findFoodsaleaddressByPrice(price, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddressByPrice(BigDecimal price, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodsaleaddressByPrice", startResult, maxRows, price);
		return new LinkedHashSet<Foodsaleaddress>(query.getResultList());
	}

	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddressByDescription(String description) throws DataAccessException {

		return findFoodsaleaddressByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddressByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodsaleaddressByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Foodsaleaddress>(query.getResultList());
	}

	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddressByDescriptionContaining(String description)
			throws DataAccessException {

		return findFoodsaleaddressByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddressByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFoodsaleaddressByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Foodsaleaddress>(query.getResultList());
	}

	@Transactional
	public Foodsaleaddress findFoodsaleaddressByPrimaryKey(Integer id) throws DataAccessException {
		return findFoodsaleaddressByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Foodsaleaddress findFoodsaleaddressByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodsaleaddressByPrimaryKey", startResult, maxRows, id);
			return (Foodsaleaddress) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddresssByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodsaleaddresssByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodsaleaddress> findAllFoodsaleaddresss() throws DataAccessException {

		return findAllFoodsaleaddresss(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodsaleaddress> findAllFoodsaleaddresss(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodsaleaddresss", startResult, maxRows);
		return new LinkedHashSet<Foodsaleaddress>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodsaleaddress> findFoodsaleaddresssByAddress(Integer addressId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFoodsaleaddresssByAddress", start, max, addressId);
		return new LinkedHashSet<Foodsaleaddress>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodsaleaddress> findFoodsaleaddresssByFood(Integer foodId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFoodsaleaddresssByFood", start, max, foodId);
		return new LinkedHashSet<Foodsaleaddress>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodsaleaddress> findFoodsaleaddresssByUnit(Integer unitId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFoodsaleaddresssByUnit", start, max, unitId);
		return new LinkedHashSet<Foodsaleaddress>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddressByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Foodsaleaddress>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Foodsaleaddress> findFoodsaleaddresssByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Foodsaleaddress.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Foodsaleaddress> foodsaleaddresss = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return foodsaleaddresss;
	}

	@Transactional
	public Foodsaleaddress findFoodsaleaddressByPhysicalPrimaryKey(Integer foodId, Integer addressId, Calendar date)
			throws DataAccessException {
		return findFoodsaleaddressByPhysicalPrimaryKey(foodId, addressId, date, -1, -1);
	}

	@Transactional
	public Foodsaleaddress findFoodsaleaddressByPhysicalPrimaryKey(Integer foodId, Integer addressId, Calendar date,
			int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodsaleaddressByPhyscialPrimaryKey", startResult, maxRows, foodId,
					addressId, date);
			return (Foodsaleaddress) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Foodsaleaddress entity) {
		return true;
	}
}
